{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**7. Reverse Integer**  <font color=green size=3>_easy_</font>\n",
    "\n",
    "Given a 32-bit signed integer, reverse digits of an integer.\n",
    "\n",
    "**Example 1**\n",
    "\n",
    "```\n",
    "Input: 123\n",
    "Output: 321\n",
    "```\n",
    "**Example 2**\n",
    "\n",
    "```\n",
    "Input: -123\n",
    "Output: -321\n",
    "```\n",
    "**Example 3**\n",
    "\n",
    "```\n",
    "Input: 120\n",
    "Output: 21\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "321\n",
      "-321\n",
      "21\n"
     ]
    }
   ],
   "source": [
    "def reverse(num):\n",
    "    \"\"\"\n",
    "    Input: 120\n",
    "    Output: 21\n",
    "    \"\"\"\n",
    "    # revert_arr = []\n",
    "    # abs_num = abs(num)\n",
    "    # while abs_num > 0:\n",
    "    #     mod = abs_num % 10\n",
    "    #     abs_num = abs_num - mod\n",
    "    #     abs_num = abs_num / 10\n",
    "    #     revert_arr.append(mod)\n",
    "    #\n",
    "    # sum = 0\n",
    "    # for index, n in enumerate(revert_arr):\n",
    "    #     sum += (n * (10 ** (len(revert_arr) - index - 1)))\n",
    "    #\n",
    "    # return sum if num > 0 else -sum\n",
    "\n",
    "    abs_num = abs(num)\n",
    "    rev = 0\n",
    "    while abs_num != 0:\n",
    "        pop = abs_num % 10\n",
    "        abs_num //= 10\n",
    "\n",
    "        rev = rev * 10 + pop\n",
    "    return rev if num > 0 else -rev\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    print(reverse(123))\n",
    "    print(reverse(-123))\n",
    "    print(reverse(120))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
